package com.quantgroup.data.metrics;

import com.quantgroup.data.util.VisitorUtil;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;

/**
 * 默认指标收集器
 * @author yangjinhua
 */
public class DefaultCollector {

    public static final Counter requestErrorCounter = Counter.build()
            .name("io_http_requests_error_total")
            .help("错误请求数")
            .labelNames("authType", "path", "exceptionType")
            .help("Total error requests.")
            .register();

    public static final Counter requestCounter = Counter.build()
            .name("io_http_requests_total")
            .help("接口请求数")
            .labelNames("clientName", "uri")
            .help("Total requests.")
            .register();

    public static final Counter sinkErrorCounter = Counter.build()
            .name("io_sink_error_total")
            .help("存储错误数")
            .labelNames("exceptionType")
            .help("Total sink error.")
            .register();

    public static final Summary requestSpendCollector = Summary.build()
            .name("rc_request_spend_statistics")
            .help("每个请求的耗时")
            .quantile(0.5, 0.05)
            .quantile(0.9, 0.01)
            .quantile(1, 0.001)
            .maxAgeSeconds(20)
            .ageBuckets(2)
            .labelNames("clientName", "uri")
            .register();

    public static final Summary sinkSpendCollector = Summary.build()
            .name("rc_sink_spend_statistics")
            .help("存储耗时")
            .quantile(0.5, 0.05)
            .quantile(0.9, 0.01)
            .quantile(1, 0.001)
            .maxAgeSeconds(20)
            .ageBuckets(2)
            .labelNames("sinker")
            .register();

    public static final Summary unitCalcSpendCollector = Summary.build()
            .name("rc_feature_unit_calc_statistics")
            .help("每个计算单元耗时")
            .quantile(0.5, 0.05)
            .quantile(0.9, 0.01)
            .quantile(1, 0.001)
            .maxAgeSeconds(20)
            .ageBuckets(2)
            .labelNames("code")
            .register();

    public static final Counter recallCalcErrorCounter = Counter.build()
            .name("recall_calc_error_total")
            .help("回溯特征计算错误数")
            .labelNames("exceptionType")
            .help("Total recall calc error.")
            .register();

    public static final Counter recallCalcSuccessCounter = Counter.build()
            .name("recall_calc_order_total")
            .help("回溯特征计算成功数")
            .labelNames("success")
            .help("Total recall calc success.")
            .register();

    public static final Counter preCalcSuccessCounter = Counter.build()
            .name("pre_calc_order_total")
            .help("预计算特征计算成功数")
            .labelNames("success")
            .help("Total pre calc success.")
            .register();

    public static final Counter modelCodeRequestCounter = Counter.build()
            .name("model_code_request_times")
            .help("model code 请求次数")
            .labelNames("visitor","code")
            .help("model code request count")
            .register();

    public static void requestErrorMetrics(String uri, String exception) {
        String visitor = VisitorUtil.getVisitor();
        requestErrorCounter.labels(visitor, uri, exception).inc();
    }

    public static void requestMetrics(String uri) {
        String visitor = VisitorUtil.getVisitor();
        requestCounter.labels(visitor, uri).inc();
    }

    public static void sinkErrorMetrics(String exception) {
        sinkErrorCounter.labels(exception).inc();
    }

    public static void recallCalcErrorMetrics(String exception) {
        recallCalcErrorCounter.labels(exception).inc();
    }

    public static void recallCalcSuccessMetrics() {
        recallCalcSuccessCounter.labels("success").inc();
    }

    public static void preCalcSuccessMetrics() {
        preCalcSuccessCounter.labels("success").inc();
    }

    public static void requestSpendMetrics(String uri, long spendTime) {
        String visitor = VisitorUtil.getVisitor();
        requestSpendCollector.labels(visitor, uri).observe(spendTime);
    }

    public static void sinkSpendMetrics(String sinkName, long spendTime) {
        sinkSpendCollector.labels(sinkName).observe(spendTime);
    }

    public static void unitCalcSpendMetrics(String codeName, long spendTime) {
        unitCalcSpendCollector.labels(codeName).observe(spendTime);
    }

    public static void modelCodeRequestMetrics(String visitor,String codeName) {
        modelCodeRequestCounter.labels(visitor,codeName).inc();
    }


}
